package com.jack.online.study.controller.edu.admin;

import com.jack.online.study.common.base.result.R;
import com.jack.online.study.common.base.result.ResultCodeEnum;
import com.jack.online.study.common.exception.GuliException;
import com.jack.online.study.common.util.ExceptionUtils;
import com.jack.online.study.model.edu.vo.SubjectVo;
import com.jack.online.study.service.edu.SubjectService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author jack
 * @since 2024-04-17
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/admin/edu/subject")
public class SubjectController {

    @Resource
    private SubjectService subjectService;

    /**
     * Excel批量导入课程类别数据
     */
    @PostMapping("import")
    public R batchImport(@RequestParam("file") MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            subjectService.batchImport(inputStream);
            return R.ok().message("批量导入成功");
        } catch (Exception e) {
            log.error(ExceptionUtils.getMessage(e));
            throw new GuliException(ResultCodeEnum.EXCEL_DATA_IMPORT_ERROR);
        }
    }

    @GetMapping("nested-list")
    public R nestedList(){
        List<SubjectVo> subjectVoList = subjectService.nestedList();
        return R.ok().data("items", subjectVoList);
    }
}
